<?php
namespace app\admin\controller;
use app\model\Banner as BannerModel;
use think\Db;


class Banner extends Base
{
    public function __construct(){
        $model = new BannerModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '轮播图';
        
        if ( in_array($page_info['action'], ['add','edit']) ) {
            $fields = [
                'title'=>['name'=>'标题','type'=>'text'],
                'description'=>['name'=>'描述','type'=>'textarea'],
                'image'=>['name'=>'图片','type'=>'file'],
                'btn_name'=>['name'=>'按钮名称','type'=>'text'],
                'url'=>['name'=>'链接','type'=>'text'],
                'sort'=>['name'=>'排序值','type'=>'text'],
                'status'=>['name'=>'状态','type'=>'select1','params'=>$model->getStatusList()],
            ];
            $valids = createPageValid($fields);
            $this->assign([
                'fields'=>$fields,
                'valids'=>$valids,
            ]);
        }

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function index(){
        $model = $this->model;
        $map_params = [
            ['key'=>'title','type'=>'like'],
        ];
        $map = getSearchParam($map_params);

        $search_params = [
            [
                ['key'=>'title','name'=>'标题','type'=>'text'],
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);

        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',20)
        ];
        $list = $model->where($map)->order('id','desc')->paginate($page_param);
        $page = $list->render();

        $list = $list->toarray();
        foreach ($list['data'] as $k => $vo) {
            
        }
        return view('index', [
            'list'=>$list,
            'page'=>$page
        ]);
    }

    public function add(){
        if (request()->isPost()) {
            $fields = [
                'must'=>['title','image'],
                'nomust'=>['sort','btn_name','url','description','status']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
        }
        return parent::saveData($save_data);
    }

    public function edit(){
        $id = input('id');
        if (empty($id)) { $this->error('id不能为空'); }
        
        if (request()->isPost()) {
            $fields = [
                'must'=>['title','image'],
                'nomust'=>['sort','btn_name','url','description','status']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
        }
        return parent::updateData($id,$save_data);
    }




   
}
